package ac.svn.accesscontrol.store;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Dictionary implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 3L;


    private boolean 			alwaysOpen 		= false;
    private static Dictionary 		dictionary 		= null;
    private Set<AccessFile> 		accessFiles ;
    private AccessFile 			lastAccessFile;
    private Map<String, UserDetails> 	userDetails;
    private Map<String, GroupDetails> 	groupDetails;
    private Map<String, LocationDetails>locationDetails;

    public static String configFileName = "smartCm.cfg";

    private Dictionary()
    {
	accessFiles 	= new HashSet<AccessFile>();
	userDetails 	= new HashMap<String, UserDetails>();
	groupDetails	= new HashMap<String, GroupDetails>();
	locationDetails = new HashMap<String, LocationDetails>();
	lastAccessFile  = new AccessFile();
    }

    public static Dictionary getDictionary()
    {
	if(null == dictionary)
	{
	    dictionary = new Dictionary();
	}		
	return dictionary;
    }


    public Set<AccessFile> getAccessFiles() {
	return accessFiles;
    }
    public void setAccessFiles(Set<AccessFile> configFiles) {
	accessFiles = configFiles;
    }
    public AccessFile getLastAccessFile() {
	return lastAccessFile;
    }
    public void setLastAccessFile(AccessFile lastConfigFile) {
	lastAccessFile = lastConfigFile;
    }

    public void addAccessFile(AccessFile accessFile)
    {
	AccessFile oldAccessFile = null;
	for(AccessFile file: accessFiles)
	{
	    if(file.getAccessFileLocation().toUpperCase().equalsIgnoreCase(accessFile.getAccessFileLocation().toUpperCase()))
	    {
		oldAccessFile = file;
	    }
	}

	if(null != oldAccessFile)
	{
	    accessFiles.remove(oldAccessFile);
	}
	accessFiles.add(accessFile);
    }

    public boolean isAlwaysOpen() {
	return alwaysOpen;
    }

    public void setAlwaysOpen(boolean alwaysOpen) {
	this.alwaysOpen = alwaysOpen;
    }

    public Map<String, UserDetails> getUserDetails() {
	return userDetails;
    }

    public UserDetails getUserDetails(String userId)
    {
	if(null != userDetails)
	{
	    return userDetails.get(userId);
	}
	return null;
    }

    public void updateUserDetails(UserDetails user)
    {
	userDetails.put(user.getUserId(),user);
    }

    public void deleteUserDetails(String userId)
    {
	userDetails.remove(userId);
    }

    public void clone(Dictionary dic)
    {
	for(AccessFile accessFile : dic.accessFiles)
	{
	    addAccessFile(accessFile);
	}

	//Copy details of user present last time.
	for(String userId : dic.getUserDetails().keySet())
	{
	    UserDetails user =dic.userDetails.get(userId);
	    userDetails.put(userId, user);
	}

	//Copy details of Groups present last time.
	for(String groupId : dic.getGroupDetails().keySet())
	{
	    GroupDetails group =dic.groupDetails.get(groupId);
	    groupDetails.put(groupId, group);
	}

	//Copy details of Locations present last time.
	for(String locationId : dic.getLocationDetails().keySet())
	{
	    LocationDetails location = dic.locationDetails.get(locationId);
	    locationDetails.put(locationId, location);
	}

	lastAccessFile.setAccessFileLocation(dic.getLastAccessFile().getAccessFileLocation());
	lastAccessFile.setAccessFileName(dic.getLastAccessFile().getAccessFileName());
	lastAccessFile.setNotes(dic.getLastAccessFile().getNotes());

	alwaysOpen = dic.isAlwaysOpen();
    }

    /**
     * @return the groupDetails
     */
    public Map<String, GroupDetails> getGroupDetails() {
	return groupDetails;
    }

    /**
     * @param groupDetails the groupDetails to set
     */
    public void setGroupDetails(Map<String, GroupDetails> groupDetails) {
	this.groupDetails = groupDetails;
    }

    /**
     * @return the locationDetails
     */
    public Map<String, LocationDetails> getLocationDetails() {
	return locationDetails;
    }

    /**
     * @param locationDetails the locationDetails to set
     */
    public void setLocationDetails(Map<String, LocationDetails> locationDetails) {
	this.locationDetails = locationDetails;
    }

    /**
     * @param userDetails the userDetails to set
     */
    public void setUserDetails(Map<String, UserDetails> userDetails) {
	this.userDetails = userDetails;
    }


}
